package com.zzhrtech.wgzx_cityzen.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.ImageView;

import com.zzhrtech.wgzx_cityzen.R;

/**
 * Created by renxiangpeng on 16/9/29.
 */
public class CustomProgress extends Dialog {

    public CustomProgress(Context context) {
        super(context);
    }

    public CustomProgress(Context context, int theme) {
        super(context, theme);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        ImageView imageView = (ImageView) findViewById(R.id.ivArrow);
        // 获取ImageView上的动画背景
        AnimationDrawable spinner = (AnimationDrawable) imageView.getBackground();
        // 开始动画
        spinner.start();
    }

    /**
     * 弹出自定义ProgressDialog
     *
     * @param context
     *            上下文
     *            提示
     * @param cancelable
     *            是否按返回键取消
     * @param cancelListener
     *            按下返回键监听
     * @return
     */
    public static CustomProgress show(Context context,  boolean cancelable, OnCancelListener cancelListener) {
        CustomProgress dialog = new CustomProgress(context,R.style.Custom_Progress);
        ImageView view = new ImageView(context);
        view.setId(R.id.ivArrow);
        AnimationDrawable drawable = (AnimationDrawable) context.getResources().getDrawable(R.drawable.frame_anim);
        view.setBackground(drawable);
        dialog.setContentView(view);
        dialog.setCancelable(cancelable);
        dialog.setOnCancelListener(cancelListener);
        dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        // 设置背景层透明度
        lp.dimAmount = 0.2f;
        dialog.getWindow().setAttributes(lp);
        return dialog;
    }
}
